
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>es6 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="../图片规范/图片规范.html" />
    
    
    <link rel="prev" href="0.javascript.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    引言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../html/0.html.html">
            
                <a href="../html/0.html.html">
            
                    
                    HTML
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../html/1.html整体结构.html">
            
                <a href="../html/1.html整体结构.html">
            
                    
                    HTML整体结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../html/2.html代码格式.html">
            
                <a href="../html/2.html代码格式.html">
            
                    
                    HTML代码格式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../html/3.html内容语义.html">
            
                <a href="../html/3.html内容语义.html">
            
                    
                    HTML内容语义
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../css/0.css.html">
            
                <a href="../css/0.css.html">
            
                    
                    CSS
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../css/1.css书写规范.html">
            
                <a href="../css/1.css书写规范.html">
            
                    
                    CSS书写规范
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../css/2.css命名规范.html">
            
                <a href="../css/2.css命名规范.html">
            
                    
                    CSS命名规范
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="0.javascript.html">
            
                <a href="0.javascript.html">
            
                    
                    JavaScript
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter active" data-level="1.4.1" data-path="1.javascript-es6.html">
            
                <a href="1.javascript-es6.html">
            
                    
                    es6
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../图片规范/图片规范.html">
            
                <a href="../图片规范/图片规范.html">
            
                    
                    图片规范
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../前端研发流程/前端研发流程.html">
            
                <a href="../前端研发流程/前端研发流程.html">
            
                    
                    前端研发流程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../项目/0.项目.html">
            
                <a href="../项目/0.项目.html">
            
                    
                    项目
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="../项目/1.食尚购.html">
            
                <a href="../项目/1.食尚购.html">
            
                    
                    食尚购App
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../文档/0.文档.html">
            
                <a href="../文档/0.文档.html">
            
                    
                    文档
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.8.1" data-path="../文档/1.食尚购.html">
            
                <a href="../文档/1.食尚购.html">
            
                    
                    食尚购App
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.2" data-path="../文档/2.门店EDI.html">
            
                <a href="../文档/2.门店EDI.html">
            
                    
                    门店EDI
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.3" data-path="../文档/3.掌中宝.html">
            
                <a href="../文档/3.掌中宝.html">
            
                    
                    掌中宝
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.4" data-path="../文档/4.食尚购小程序.html">
            
                <a href="../文档/4.食尚购小程序.html">
            
                    
                    食尚购小程序
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >es6</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="javascript-es6-&#x7F16;&#x7801;&#x89C4;&#x8303;">JavaScript ES6 &#x7F16;&#x7801;&#x89C4;&#x8303;</h1>
<p>&#x6B64;&#x4EE3;&#x7801;&#x89C4;&#x8303;&#x4E3A;&#x76EE;&#x524D;&#x8F83;&#x4E3A;&#x6D41;&#x884C;&#x7684; Airbnb js &#x4EE3;&#x7801;&#x89C4;&#x8303;</p>
<blockquote>
<p>&#x539F;&#x6587;&#x5730;&#x5740;&#xFF1A; <a href="https://github.com/airbnb/javascript" target="_blank">https://github.com/airbnb/javascript</a></p>
</blockquote>
<h3 id="&#x7C7B;&#x578B;">&#x7C7B;&#x578B;</h3>
<p>1.1 &#x539F;&#x59CB;&#x7C7B;&#x578B;</p>
<ul>
<li>string</li>
<li>number</li>
<li>boolean</li>
<li>null</li>
<li>undefined</li>
<li>symbol</li>
</ul>
<pre><code>const foo = 1;
let bar = foo;
bar = 9;
console.log(foo, bar); // =&gt; 1 9
</code></pre><p>1.2 &#x5F15;&#x7528;&#x7C7B;&#x578B;</p>
<ul>
<li>object</li>
<li>array</li>
<li>function</li>
</ul>
<pre><code>const foo = [1, 2];
const bar = foo;
bar[0] = 9;
console.log(foo[0], bar[0]); // =&gt; 9 9
</code></pre><h3 id="&#x5F15;&#x7528;">&#x5F15;&#x7528;</h3>
<p>2.1 &#x5BF9;&#x6240;&#x6709;&#x7684;&#x5F15;&#x7528;&#x4F7F;&#x7528; const&#xFF0C;&#x907F;&#x514D;&#x4F7F;&#x7528;var&#x3002;eslint: prefer-const, no-const-assign</p>
<blockquote>
<p>&#x8FD9;&#x6837;&#x80FD;&#x786E;&#x4FDD;&#x4F60;&#x4E0D;&#x80FD;&#x5BF9;&#x5F15;&#x7528;&#x91CD;&#x65B0;&#x8D4B;&#x503C;&#xFF0C;&#x4E0D;&#x4F1A;&#x56E0;&#x6B64;&#x51FA;&#x73B0; bug &#x6216;&#x96BE;&#x4EE5;&#x7406;&#x89E3;&#x7684;&#x4EE3;&#x7801;</p>
</blockquote>
<pre><code>// bad
var a = 1;
var b = 2;

// good
const a = 1;
const b = 2;
</code></pre><p>2.2 &#x5982;&#x679C;&#x4F60;&#x5FC5;&#x987B;&#x5BF9;&#x5F15;&#x7528;&#x8D4B;&#x503C;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;let&#x4EE3;&#x66FF;var&#x3002; eslint: no-var jscs: disallowVar</p>
<blockquote>
<p>&#x56E0;&#x4E3A; let &#x662F;&#x5757;&#x7EA7;&#x4F5C;&#x7528;&#x57DF;&#xFF0C;&#x800C; var &#x662F;&#x51FD;&#x6570;&#x4F5C;&#x7528;&#x57DF;</p>
</blockquote>
<pre><code>// bad
var count = 1;
if (true) {
    count += 1;
}

// good, use the let.
let count = 1;
if (true) {
    count += 1;
}
</code></pre><p>2.3 &#x6CE8;&#x610F;&#xFF1A;let &#x548C; const &#x90FD;&#x662F;&#x5757;&#x7EA7;&#x4F5C;&#x7528;&#x57DF;&#x3002;</p>
<pre><code>// const &#x548C; let &#x53EA;&#x5728;&#x4ED6;&#x5B9A;&#x4E49;&#x7684;&#x5757;&#x7EA7;&#x4F5C;&#x7528;&#x57DF;&#x5185;&#x5B58;&#x5728;
{
  let a = 1;
  const b = 1;
}
console.log(a); // ReferenceError
console.log(b); // ReferenceError
</code></pre><h3 id="&#x5BF9;&#x8C61;">&#x5BF9;&#x8C61;</h3>
<p>3.1 &#x4F7F;&#x7528;&#x5BF9;&#x8C61;&#x5B57;&#x9762;&#x91CF;&#x521B;&#x5EFA;&#x5BF9;&#x8C61;. eslint: no-new-object</p>
<pre><code>// bad
const item = new Object();

// good
const item = {};
</code></pre><p>3.2 &#x5728;&#x521B;&#x5EFA;&#x5177;&#x6709;&#x52A8;&#x6001;&#x5C5E;&#x6027;&#x540D;&#x7684;&#x5BF9;&#x8C61;&#x65F6;&#xFF0C;&#x4F7F;&#x7528;&#x8BA1;&#x7B97;&#x5C5E;&#x6027;&#x540D;</p>
<blockquote>
<p>&#x5141;&#x8BB8;&#x60A8;&#x5728;&#x4E00;&#x4E2A;&#x5730;&#x65B9;&#x5B9A;&#x4E49;&#x5BF9;&#x8C61;&#x7684;&#x6240;&#x6709;&#x5C5E;&#x6027;</p>
</blockquote>
<pre><code>function getKey(k) {
  return `a key named ${k}`;
}

// bad
const obj = {
  id: 5,
  name: &apos;San Francisco&apos;,
};
obj[getKey(&apos;enabled&apos;)] = true;

// good
const obj = {
  id: 5,
  name: &apos;San Francisco&apos;,
  [getKey(&apos;enabled&apos;)]: true,
};
</code></pre><p>3.3 &#x4F7F;&#x7528;&#x5BF9;&#x8C61;&#x65B9;&#x6CD5;&#x7684;&#x7B80;&#x5199;. eslint: object-shorthand jscs: requireEnhancedObjectLiterals</p>
<pre><code>// bad
const atom = {
    value: 1,
    addValue: function (value) {
        return this.value + value;
    },
};

// good
const atom = {
    value: 1,
    addValue (value) {
        return this.value + value;
    },
};
</code></pre><p>3.4 &#x4F7F;&#x7528;&#x5BF9;&#x8C61;&#x5C5E;&#x6027;&#x503C;&#x7B80;&#x5199;. eslint: object-shorthand jscs: requireEnhancedObjectLiterals</p>
<blockquote>
<p>&#x8FD9;&#x6837;&#x66F4;&#x7B80;&#x77ED;&#x4E14;&#x5177;&#x6709;&#x63CF;&#x8FF0;&#x6027;.</p>
</blockquote>
<pre><code>const lukeSkywalker = &apos;Luke Skywalker&apos;;

// bad
const obj = {
    lukeSkywalker: lukeSkywalker,
};

// good
const obj = {
    lukeSkywalker,
};
</code></pre><p>3.5 &#x5728;&#x5BF9;&#x8C61;&#x58F0;&#x660E;&#x5F00;&#x59CB;&#x7684;&#x4F4D;&#x7F6E;&#x5C06;&#x60A8;&#x7684;&#x7B80;&#x5199;&#x5C5E;&#x6027;&#x5206;&#x7EC4;&#x3002;</p>
<blockquote>
<p>&#x8FD9;&#x6837;&#x53EF;&#x4EE5;&#x66F4;&#x5BB9;&#x6613;&#x544A;&#x77E5;&#x54EA;&#x4E9B;&#x5C5E;&#x6027;&#x4F7F;&#x7528;&#x4E86;&#x7B80;&#x5199;&#x3002;</p>
</blockquote>
<pre><code>const lukeSkywalker = &apos;Luke Skywalker&apos;;
const anakinSkywalker = &apos;Anakin Skywalker&apos;;

// bad
const obj = {
  episodeOne: 1,
  twoJediWalkIntoACantina: 2,
  lukeSkywalker,
  episodeThree: 3,
  mayTheFourth: 4,
  anakinSkywalker,
};

// good
const obj = {
  lukeSkywalker,
  anakinSkywalker,
  episodeOne: 1,
  twoJediWalkIntoACantina: 2,
  episodeThree: 3,
  mayTheFourth: 4,
};
</code></pre><p>3.6 &#x53EA;&#x6709;&#x5BF9;&#x65E0;&#x6548;&#x7684;&#x6807;&#x8BC6;&#x7B26;&#x7684;&#x5C5E;&#x6027;&#x624D;&#x4F7F;&#x7528;&#x5F15;&#x53F7;. eslint: quote-props jscs:disallowQuotedKeysInObjects</p>
<blockquote>
<p>&#x603B;&#x7684;&#x6765;&#x8BF4;&#xFF0C;&#x6211;&#x4EEC;&#x8BA4;&#x4E3A;&#x8FD9;&#x6837;&#x9605;&#x8BFB;&#x66F4;&#x5BB9;&#x6613;&#x3002;&#x5B83;&#x6539;&#x8FDB;&#x4E86;&#x8BED;&#x6CD5;&#x9AD8;&#x4EAE;&#x663E;&#x793A;&#xFF0C;&#x5E76;&#x4E14;&#x66F4;&#x5BB9;&#x6613;&#x88AB;&#x8BB8;&#x591A;JS&#x5F15;&#x64CE;&#x4F18;&#x5316;&#x3002;</p>
</blockquote>
<pre><code>// bad
const bad = {
    &apos;foo&apos;: 3,
    &apos;bar&apos;: 4,
    &apos;data-blah&apos;: 5,
};

// good
const good = {
    foo: 3,
    bar: 4,
    &apos;data-blah&apos;: 5,
};
</code></pre><p>3.7 &#x4E0D;&#x8981;&#x76F4;&#x63A5;&#x8C03;&#x7528; Object.prototype &#x7684;&#x65B9;&#x6CD5;&#xFF0C;&#x6BD4;&#x5982; hasOwnProperty&#x3001;propertyIsEnumerable&#x3001;isPrototypeOf&#x3002;</p>
<blockquote>
<p>&#x8FD9;&#x4E9B;&#x65B9;&#x6CD5;&#x53EF;&#x80FD;&#x88AB;&#x8003;&#x8651;&#x4E4B;&#x4E2D;&#x7684;&#x5BF9;&#x8C61;&#x5C5E;&#x6027;&#x6240;&#x5C4F;&#x853D;&#xFF0C;&#x8003;&#x8651;&#x4E0B; { hasOwnProperty: false } &#x6216;&#x8005;&#x521B;&#x5EFA;&#x4E86;&#x4E00;&#x4E2A;&#x7A7A;&#x5BF9;&#x8C61;&#xFF08;Object.create(null)&#xFF09;</p>
</blockquote>
<pre><code>// bad
console.log(object.hasOwnProperty(key));

// good
console.log(Object.prototype.hasOwnProperty.call(object, key));

// best
const has = Object.prototype.hasOwnProperty; // cache the lookup once, in module scope.
// or
import has from &apos;has&apos;; // https://www.npmjs.com/package/has
// ...
console.log(has.call(object, key));
</code></pre><p>3.8 &#x6D45;&#x62F7;&#x8D1D;&#x5BF9;&#x8C61;&#x7684;&#x65F6;&#x5019;&#x4F7F;&#x7528; ... &#x64CD;&#x4F5C;&#x7B26;&#x800C;&#x4E0D;&#x662F; Object.assign</p>
<pre><code>// very bad
const original = { a: 1, b: 2 };
const copy = Object.assign(original, { c: 3 }); // this mutates `original` &#xCA0;_&#xCA0;
delete copy.a; // so does this

// bad
const original = { a: 1, b: 2 };
const copy = Object.assign({}, original, { c: 3 }); // copy =&gt; { a: 1, b: 2, c: 3 }

// good
const original = { a: 1, b: 2 };
const copy = { ...original, c: 3 }; // copy =&gt; { a: 1, b: 2, c: 3 }

const { a, ...noA } = copy; // noA =&gt; { b: 2, c: 3 }
</code></pre>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="0.javascript.html" class="navigation navigation-prev " aria-label="Previous page: JavaScript">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="../图片规范/图片规范.html" class="navigation navigation-next " aria-label="Next page: 图片规范">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"es6","level":"1.4.1","depth":2,"next":{"title":"图片规范","level":"1.5","depth":1,"path":"图片规范/图片规范.md","ref":"图片规范/图片规范.md","articles":[]},"previous":{"title":"JavaScript","level":"1.4","depth":1,"path":"js/0.javascript.md","ref":"js/0.javascript.md","articles":[{"title":"es6","level":"1.4.1","depth":2,"path":"js/1.javascript-es6.md","ref":"js/1.javascript-es6.md","articles":[]}]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"js/1.javascript-es6.md","mtime":"2019-10-12T05:14:45.496Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-07-02T00:51:07.028Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

